{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 单链表\n",
    "class ListNode:\n",
    "    def __init__(self, val=None, next=None):\n",
    "        self.data = val\n",
    "        self.next = next\n",
    "class LinkedList:\n",
    "    def __init__(self):\n",
    "        self.dummy_head = ListNode()\n",
    "        self.size = 0\n",
    "    \n",
    "    def create(self, List):\n",
    "        for val in List:\n",
    "            last_node = self.dummy_head\n",
    "            while last_node.next:\n",
    "                last_node = last_node.next\n",
    "            last_node.next = ListNode(val)\n",
    "            self.size +=1\n",
    "            \n",
    "    def print(self):\n",
    "        cur = self.dummy_head\n",
    "        while cur:\n",
    "            print(cur.data, end=' -> ')\n",
    "            cur = cur.next\n",
    "        print('None')\n",
    "        print(self.size)\n",
    "            \n",
    "    def algorithm(self, k):\n",
    "        pre = self.dummy_head\n",
    "        while pre:\n",
    "            test = pre\n",
    "            for _ in range(k):\n",
    "                test = test.next\n",
    "                if test == None:           # 当存在None，说明当前序列不满K个\n",
    "                    return                 # 判断终止条件\n",
    "                \n",
    "            cur = pre.next\n",
    "            for _ in range(k-1):\n",
    "                tmp = cur.next\n",
    "                cur.next = tmp.next\n",
    "                tmp.next = pre.next\n",
    "                pre.next = tmp             # 中间K个反转链表代码\n",
    "            pre = cur                      # 将pre向后移动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "LinkedList = LinkedList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = list(range(10))\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> None\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "LinkedList.create(nums)\n",
    "LinkedList.print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 2 -> 1 -> 0 -> 5 -> 4 -> 3 -> 8 -> 7 -> 6 -> 9 -> None\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "LinkedList.algorithm(3)\n",
    "LinkedList.print()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
